import { _decorator, Component, instantiate, Node, Prefab, SpriteFrame, UITransform, v3, Widget } from 'cc';
import { Tools } from '../game/Tools';
import { IslandItem } from './IslandItem';
const { ccclass, property } = _decorator;

@ccclass('IslandPage')
export class IslandPage extends Component {

    @property(Node)
    content: Node = null;

    @property(Prefab)
    island_pre: Prefab = null;


    @property(SpriteFrame)
    title_sprf: SpriteFrame[] = [];
    @property(SpriteFrame)
    island_icon: SpriteFrame[] = [];

    start() {
        let island_data = [{ id: 1, name: "珍珠岛", unlock: 0 }, { id: 2, name: "爱情岛", unlock: -1 }, { id: 3, name: "加勒比海岛", unlock: -1 }]//模拟数据 0 正在进行 ，1 已解锁 ，-1 未解锁

        this.new_Island(island_data);
    }

    new_Island(unlock: any[]) {
        for (let i = 0; i < unlock.length; i++) {
            let _island = instantiate(this.island_pre);
            _island.setParent(this.content)

            const u = unlock[i];
            _island.getComponent(IslandItem).set_Island(this.title_sprf[i], this.island_icon[i], u.unlock)
        }
    }

    onOpen() {
        Tools.updateWidget(this.node, true)

    }

    onClose() {
        this.node.getComponent(Widget).enabled = false;
        this.node.setPosition(v3(1000, 0, 0));
    }

    on_trun_page(e, str: string) {
        let wid = this.content.parent.getComponent(UITransform).contentSize.x
        let child_num = this.content.children.length
        let total_wid = wid * (child_num - 1)
        let cur_x = this.content.x;
        
        if (str == "right") {
            let val = (cur_x - wid) <= -total_wid ? -total_wid :(cur_x - wid);
            this.content.setPosition(val, 0)
        }
        else if (str == "left") {
            let val = (cur_x + wid) >= 0 ? 0 :(cur_x + wid);
            this.content.setPosition(val, 0)
        }
    }
}


